package com.java.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.java.domain.KsDo;
import com.java.domain.PatientDo;
import com.java.domain.RoleDo;
import com.java.domain.UserDo;
import com.java.service.KsService;
import com.java.service.PatientService;
import com.java.service.RoleService;
import com.java.service.UserService;
import com.java.utils.JsonUtils;
import com.java.utils.StrUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport implements ModelDriven<UserDo> {

	private static final long serialVersionUID = 1L;

	@Resource
	private UserService userservice;

	@Resource
	private RoleService roleService;

	@Resource
	private PatientService patientService;

	@Resource
	private KsService ksService;

	private UserDo model = new UserDo();

	public String login() {
		return "login";
	}

	// 登录操作
	public String loginOpt() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if ("admin".equals(username) && "123456".equals(password)) {
			UserDo userDo = new UserDo();
			userDo.setName(username);
			userDo.setPsw(password);
			userDo.setPower(3);
			request.getSession().setAttribute("userDo", userDo);
			return "loginsuccess";
		}
		if (StrUtils.isNotEmpty(username) && StrUtils.isNotEmpty(password)) {
			UserDo userDo = userservice.queryName(username);
			if (userDo != null
					&& StrUtils.md5Utils(password).equals(userDo.getPsw())) {
				request.getSession().setAttribute("userDo", userDo);
				return "loginsuccess";
			}
		}
		return "login";
	}

	// 退出
	public String logout() {
		ServletActionContext.getRequest().getSession()
				.setAttribute("userDo", null);
		return "login";
	}

	public void userSave() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String age = request.getParameter("age");
		String zy = request.getParameter("zy");
		String sxks = request.getParameter("sxks");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String opt = request.getParameter("opt");
		if (StrUtils.isNotEmpty(opt) && "opt".equals(opt)) {
			UserDo tepUser = userservice.queryName(name);
			tepUser.setAge(Integer.parseInt(age));
			tepUser.setSex(Integer.parseInt(sex));
			tepUser.setAddress(address);
			tepUser.setEmail(email);
			tepUser.setIsdoc(zy.equals("ys") ? 1 : 0);
			tepUser.setName(name);
			KsDo ks = ksService.queryByname(sxks);
			if (ks == null) {
				tepUser.setKsid(Integer.parseInt(sxks));
			} else {
				tepUser.setKsid(ks.getId());
			}
			tepUser.setMobile(mobile);
			tepUser.setPsw(StrUtils.md5Utils(mobile));
			userservice.update(tepUser);
			map.put("success", "success");

		} else if (userservice.queryName(name) == null) {
			UserDo userDo = new UserDo();
			userDo.setAge(Integer.parseInt(age));
			userDo.setSex(Integer.parseInt(sex));
			userDo.setAddress(address);
			userDo.setEmail(email);
			userDo.setIsdoc(zy.equals("ys") ? 1 : 0);
			userDo.setName(name);
			userDo.setKsid(Integer.parseInt(sxks));
			userDo.setMobile(mobile);
			userDo.setPsw(StrUtils.md5Utils(mobile));
			userservice.save(userDo);
			RoleDo roleDo = new RoleDo();
			roleDo.setRoleName(name);
			roleDo.setRolePower(0);
			UserDo temp_user = userservice.queryName(name);
			roleDo.setUid(temp_user.getId());
			roleDo.setRoleName("普通用户");
			roleService.save(roleDo);
			if (temp_user.getIsdoc() != 1) {
				PatientDo patient = new PatientDo();
				patient.setPuid(temp_user.getId());
				UserDo u = (UserDo) request.getSession().getAttribute("userDo");
				if (u != null) {
					patient.setPdid(u.getId());
				}
				patientService.save(patient);
			}
			map.put("success", "success");
		} else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	public void update() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if (StrUtils.isNotEmpty(id)) {
			UserDo userDo = userservice.getById(Integer.parseInt(id));
			if (userDo != null) {
				KsDo ksDo = ksService.getById(userDo.getKsid());
				userDo.setDname(ksDo.getDname());
				userDo.setKsid(ksDo.getId());
				map.put("userDo", userDo);
			}
		}
		JsonUtils.JsonTostring(map);
	}

	public String list() {
		return "list";
	}

	public String showUserManage() {
		return "showUserManage";
	}

	public void userlist() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		String value = request.getParameter("value");
		if (name.equals("undefined") && value.equals("undefined")
				|| ("all".equals(name) && !StrUtils.isNotEmpty(value))) {
			List<UserDo> list = userservice.findAll();
			JsonUtils.JsonTostring(list);
		} else if (("all".equals(name) && StrUtils.isNotEmpty(value))) {
			List<UserDo> list = userservice.queryAllMobile(value);
			JsonUtils.JsonTostring(list);
		} else {
			Integer isdoc = 0;
			if ("ys".equals(name)) {
				isdoc = 1;
			}
			List<UserDo> tempList = userservice.queryAll(isdoc, value);
			JsonUtils.JsonTostring(tempList);
		}
	}

	public void delete() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if (StrUtils.isNotEmpty(id)) {
			userservice.delete(Integer.parseInt(id));
			map.put("success", "success");
		} else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	public void showAllPatient() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		System.out.println(name);
		List<UserDo> userList = userservice.queryAllPatient(0);
		for (Iterator<UserDo> iterator = userList.iterator(); iterator
				.hasNext();) {
			UserDo u = iterator.next();
			if (!u.getName().equals(name)&&!name.equals("undefined")) {
				iterator.remove();
			}
		}
		JsonUtils.JsonTostring(userList);
	}

	public void showDoctorList() {
		List<UserDo> userList = userservice.queryAllPatient(1);
		JsonUtils.JsonTostring(userList);
	}

	public void showOne() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		UserDo tempUser = userservice.getById(Integer.parseInt(id));
		JsonUtils.JsonTostring(tempUser);
	}

	public UserDo getModel() {
		return model;
	}

}
